import re

"""正则表达式提取替换变量"""
def re_sub(resource_data: str, split_content: dict):
    """
    :param resource_data: 被替换的原数据
    :param split_content: 需要替换的内容
    """
    matches = re.findall(r"(?<=\$\{).*?(?=\})", resource_data)  # 提取需要替换的目标
    # print(matches)
    new_data = None  # 替换后的数据
    for i in matches:  # 替换过程
        new_value = split_content[i]
        if i == matches[0]:
            new_data = resource_data.replace("${" + i + "}", str(new_value))
        else:
            new_data = new_data.replace("${" + i + "}", str(new_value))
    if new_data is None:
        return resource_data
    return new_data

def re_get(data: str,ref):
    """
    :param data: 需要提取的原数据
    :param ref: 正则表达式
    """
    matches = re.findall(ref, data)
    return matches
# 开始调试
# old_data = '{ "data": ${code},}'
# # old_data = '123123123'
# spilt_data = {"token": "123asf6549qewA+-*asd", "user_id": [1, 2, 3, 4], "code": 1356}
#
# new_data0 = re_sub(old_data, spilt_data)
# print(new_data0)  # {"access-token": "123asf6549qewA+-*asd", "data": [1, 2, 3, 4], "phone_code": 1356}
# data_dict = eval(new_data0)
# print(type(data_dict["access-token"]))  # <class 'str'>
# print(type(data_dict["data"]))  # <class 'list'>
# print(type(data_dict["phone_code"]))  # <class 'int'>

# strdata = 'run ren携程网can den  '
# newstr = re.findall('[\u4e00-\u9fa5]{3}',strdata)
# print(newstr)